package com.eimf.saf.security.action;

import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.eimf.saf.base.action.BaseAction;
import com.eimf.saf.base.querycomm.GenerateSearchUtil;
import com.eimf.saf.base.querycomm.SearchExpression;
import com.eimf.saf.base.strutsext.JsonResponseResult;
import com.eimf.saf.base.util.Page;
import com.eimf.saf.security.entity.Position;
import com.eimf.saf.security.exception.AppException;
import com.eimf.saf.security.service.PositionService;

/**
 * @author eden
 * @date 2013-12-28 下午3:48:48
 * @Description: 职位Action
 */
public class PositionAction extends BaseAction
{

    private static final long serialVersionUID = 1L;

    private static Log logger = LogFactory.getLog(PositionAction.class);

    public Position _paraPosition = new Position();

    public Page page = new Page();

    public String pageNo;

    @Autowired
    private PositionService positionServ;

    /**
     * 
     * 查询指定条件职位信息
     * 
     * @return String
     * @exception
     * @since 1.0.0
     */
    public String doQueryList()
    {
        JsonResponseResult responseResult = JsonResponseResult.createSuccess();
        try
        {
            final SearchExpression searchExp = GenerateSearchUtil.buildSearchExp(_paraPosition);
            page.setPageNo(pageNo);
            positionServ.findListToPage(searchExp, page);
            super.jsonResult = JSONObject.fromObject(page).toString();
        } catch (final AppException app)
        {
            PositionAction.logger.error(app);
            final String msg = this.getText(app.getMessage());
            responseResult = JsonResponseResult.createFalied(msg);
            super.jsonResult = JSONObject.fromObject(responseResult).toString();
        } catch (final Exception e)
        {
            PositionAction.logger.error(e);
            final String msg = this.getText("SAF.action.Query.exception");
            responseResult = JsonResponseResult.createFalied(msg);
            super.jsonResult = JSONObject.fromObject(responseResult).toString();
        }
        PositionAction.logger.debug(super.jsonResult);
        return BaseAction.JSON;
    }

    /**
     * 新增职位
     * 
     * @return String
     * @exception
     * @since 1.0.0
     */
    public String addRole()
    {
        JsonResponseResult responseResult = JsonResponseResult.createSuccess();
        try
        {
            positionServ.addData(_paraPosition);
        } catch (final AppException app)
        {
            PositionAction.logger.error(app);
            final String msg = this.getText(app.getMessage());
            responseResult = JsonResponseResult.createFalied(msg);
        } catch (final Exception e)
        {
            PositionAction.logger.error(e);
            final String msg = this.getText("SAF.action.Add.exception");
            responseResult = JsonResponseResult.createFalied(msg);
        }
        super.jsonResult = JSONObject.fromObject(responseResult).toString();
        PositionAction.logger.debug(super.jsonResult);
        return BaseAction.JSON;
    }

    /**
     * 修改职位
     * 
     * @return String
     * @exception
     * @since 1.0.0
     */
    public String updRole()
    {
        JsonResponseResult responseResult = JsonResponseResult.createSuccess();
        try
        {
            positionServ.updateData(_paraPosition);
        } catch (final AppException app)
        {
            PositionAction.logger.error(app);
            final String msg = this.getText(app.getMessage());
            responseResult = JsonResponseResult.createFalied(msg);
        } catch (final Exception e)
        {
            PositionAction.logger.error(e);
            final String msg = this.getText("SAF.action.Upd.exception");
            responseResult = JsonResponseResult.createFalied(msg);
        }
        super.jsonResult = JSONObject.fromObject(responseResult).toString();
        PositionAction.logger.debug(super.jsonResult);
        return BaseAction.JSON;
    }

    /**
     * 删除职位
     * 
     * @return String
     * @exception
     * @since 1.0.0
     */
    public String delMenu()
    {
        JsonResponseResult responseResult = JsonResponseResult.createSuccess();
        try
        {
            positionServ.delData(_paraPosition);
        } catch (final AppException app)
        {
            PositionAction.logger.error(app);
            final String msg = this.getText(app.getMessage());
            responseResult = JsonResponseResult.createFalied(msg);
        } catch (final Exception e)
        {
            PositionAction.logger.error(e);
            final String msg = this.getText("SAF.action.Del.exception");
            responseResult = JsonResponseResult.createFalied(msg);
        }
        super.jsonResult = JSONObject.fromObject(responseResult).toString();
        PositionAction.logger.debug(super.jsonResult);
        return BaseAction.JSON;
    }
}
